package com.matt.controller.pad.care;

import com.matt.service.care.HLCareBaseService;
import com.matt.service.care.po.HLCareBaseRecordPO;
import com.starinside.wp.components.MemVerificationCache;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;

//import static com.matt.foundation.constant.RequestConstant.TICKET;

public class CareBaseController<T1, T2 extends HLCareBaseService<T1, P>, P extends HLCareBaseRecordPO> {

    protected T2 baseService;
    public void setBaseService(T2 service){
        this.baseService = service;
    }

    @Autowired
    protected MemVerificationCache cache;

    protected String validChildrenId(String childId, HttpServletRequest req){
        if(StringUtils.isEmpty(childId)) {
            WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

            if (loginUser.getDefaultBabyId() == null) {
                throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "当前家长没有就读的宝宝");
            }

            childId = loginUser.getDefaultBabyId();
        }

        if (StringUtils.isEmpty(childId)){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "没有指定宝宝");
        }

        return childId;
    }

    @ApiOperation(value = "获取单日列表")
    @GetMapping("/daily_records")
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, HttpServletRequest req, HttpServletResponse resp,
            String childId, Date daily){

        childId = validChildrenId(childId, req);

        List<T1>  ret = baseService.dailyRecords(null, null, childId, daily);

        return StatusResponse.success(ret);
    }

    @ApiOperation(value = "获取列表")
    @GetMapping("/list")
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, HttpServletRequest req, HttpServletResponse resp,
            String childId, PagerQuery pagerQuery){

        childId = validChildrenId(childId, req);

        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());

        return StatusResponse.success( baseService.page(null, null, childId, pageRequest));
    }


    @ApiOperation(value = "获取一个对象")
    @GetMapping()
    public StatusResponse get(@RequestHeader(TICKET) String ticket, HttpServletRequest req, String childId, String id){

        childId = validChildrenId(childId, req);

        return StatusResponse.success(baseService.get(null, null, childId, id));
    }


}
